package com.leave.mos.controller.simple;

import com.leave.mos.constant.TablenameConstant;
import org.springframework.data.mongodb.core.schema.JsonSchemaObject.Type;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ComparisonQueryOperatorsController
 * @Description TODO
 * @Author lh
 * @Date 2021/7/12 16:50
 * @Version 1.0
 *
 * ElementQueryOperators 元素查询运算符¶
 *  存在：exists
 *  类型：type
 *
 **/
@RestController
@RequestMapping("element")
public class ElementQueryOperatorsController {

    @Resource
    private MongoTemplate mongoTemplate;


    /**
     *  当<boolean>为 true 时，$exists匹配包含该字段的文档，包括字段值为 的文档 null。
     *  如果<boolean>为 false，则查询仅返回不包含该字段的文档
     */
    @RequestMapping("exists")
    public List<Map> exists(){

        //
        Criteria criteria1 = new Criteria(TablenameConstant.goods_order.orderCode).exists(false);
        Query query = new Query();
        query.addCriteria(criteria1);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

    @RequestMapping("type")
    public List<Map> type(){

        // 根据判断字段的类型，进行筛选过滤
        Criteria criteria1 = new Criteria(TablenameConstant.goods_order.orderCode).type(Type.DOUBLE);
        Query query = new Query();
        query.addCriteria(criteria1);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

    @RequestMapping("type1")
    public List<Map> type1(){

        // 根据判断字段的类型，进行筛选过滤
        //查询 字段为 数组的并且 数组里面是字符串的文档
        Criteria criteria1 = new Criteria(TablenameConstant.goods_order.boxCode).type(Type.arrayType()).type(Type.intType());
        Query query = new Query();
        query.addCriteria(criteria1);

        List<Map> maps = mongoTemplate.find(query,Map.class, TablenameConstant.goods_order.TableName);
        return maps;
    }

}
